/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.user.dto.AppUserApiKeyAddDTO;
import com.pig4cloud.pigx.user.dto.AppUserApiKeyEditDTO;
import com.pig4cloud.pigx.user.entity.UserApiKey;
import com.pig4cloud.pigx.user.service.UserApiKeyService;
import com.pig4cloud.pigx.user.vo.AppUserApiKeyAddResVO;
import com.pig4cloud.pigx.user.vo.AppUserApiKeyDetailVO;
import com.pig4cloud.pigx.user.vo.AppUserApiKeyPageVO;
import com.pig4cloud.pigx.user.vo.UserApiKeyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * Dapp端用户apiKey
 *
 * @since 2022-10-28 15:47:24
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/app/userapikey")
@Api(value = "userapikey", tags = "Dapp端用户apiKey管理")
public class AppUserApiKeyController {

	private final UserApiKeyService userApiKeyService;

	/**
	 * 分页查询
	 *
	 * @param page      分页条件
	 * @param condition 查询条件
	 * @return {@link R< Page< UserApiKeyVO>>}
	 * @since 2022/10/28 16:52
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R<Page<AppUserApiKeyPageVO>> page(Page<UserApiKey> page, UserApiKey condition) {
		return R.ok(userApiKeyService.pageApp(page, condition));
	}

	/**
	 * 删除全部
	 *
	 * @return {@link R< Page< UserApiKeyVO>>}
	 * @since 2022/10/28 16:52
	 */
	@ApiOperation(value = "删除全部", notes = "删除全部")
	@DeleteMapping("/all")
	public R<Boolean> removeAll() {
		userApiKeyService.removeAll();
		return R.ok();
	}

	/**
	 * 删除单条
	 *
	 * @return {@link R< Page< UserApiKeyVO>>}
	 * @since 2022/10/28 16:52
	 */
	@ApiOperation(value = "删除单条", notes = "删除单条")
	@DeleteMapping("/{id}")
	public R<Boolean> remove(@PathVariable Long id) {
		userApiKeyService.remove(id);
		return R.ok();
	}

	/**
	 * 根据id查询详情
	 *
	 * @param id id
	 * @return {@link R< AppUserApiKeyDetailVO>}
	 * @since 2022/11/1 18:44
	 */
	@ApiOperation(value = "根据id查询详情", notes = "根据id查询详情")
	@GetMapping("/{id}")
	public R<AppUserApiKeyDetailVO> detail(@PathVariable("id") Long id) {
		AppUserApiKeyDetailVO vo = userApiKeyService.appDetail(id);
		return R.ok(vo);
	}

	/**
	 * 新增
	 *
	 * @param dto dto
	 * @return {@link R< AppUserApiKeyAddResVO>}
	 * @since 2022/11/1 18:44
	 */
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping
	public R<AppUserApiKeyAddResVO> add(@Validated @RequestBody AppUserApiKeyAddDTO dto) {
		AppUserApiKeyAddResVO vo = userApiKeyService.add(dto);
		return R.ok(vo);
	}

	/**
	 * ApiKey创建时获取邮箱验证码
	 *
	 * @param time 客户端当前时间字符串
	 * @param name ApiKey的标签
	 * @return {@link R< Boolean>}
	 * @since 2022/11/1 18:44
	 */
	@ApiOperation(value = "ApiKey创建时获取邮箱验证码", notes = "ApiKey创建时获取邮箱验证码")
	@GetMapping("/create/verify-code")
	public R<Boolean> verifyCodeWhenCreate(
			HttpServletRequest request,
			@NotBlank(message = "参数异常，time为空") String time,
			@NotBlank(message = "参数异常，name为空") String name) {

		userApiKeyService.verifyCodeWhenCreate(request,time, name);

		return R.ok();
	}

	/**
	 * 修改
	 *
	 * @param dto dto
	 * @return {@link R< Boolean>}
	 * @since 2022/11/1 18:44
	 */
	@ApiOperation(value = "修改", notes = "修改")
	@PutMapping
	public R<Boolean> edit(@Validated @RequestBody AppUserApiKeyEditDTO dto) {
		userApiKeyService.edit(dto);
		return R.ok();
	}

	/**
	 * ApiKey提现权限修改为允许时获取邮箱验证码
	 *
	 * @param time 客户端当前时间字符串
	 * @param name ApiKey的标签
	 * @return {@link R< Boolean>}
	 * @since 2022/11/1 18:44
	 */
	@ApiOperation(value = "ApiKey提现权限修改为允许时获取邮箱验证码", notes = "ApiKey提现权限修改为允许时获取邮箱验证码")
	@GetMapping("/modify/verify-code")
	public R<Boolean> verifyCodeWhenModify(@NotBlank(message = "参数异常，time为空") String time,
										   @NotBlank(message = "参数异常，name为空") String name) {

		userApiKeyService.verifyCodeWhenModify(time, name);

		return R.ok();
	}


	/**
	 * 根据 accessKey 查询 secretKey
	 * @param accessKey accessKey
	 * @return r
	 */
	@GetMapping("/secretKey")
	public R<String> getSecretKey(@RequestParam("accessKey") String accessKey, @RequestParam("verifyCode") String verifyCode) {
		return userApiKeyService.getSecretKey(accessKey,verifyCode);
	}

	/**
	 * 用户查询 secretKey 时发送邮件
	 *
	 * @param time 客户端当前时间字符串
	 * @param name 标签
	 * @return r
	 */
	@GetMapping("/secretKey/verify-code")
	public R verifyCodeWhenGetSecretKey(@RequestParam("time") String time,@RequestParam("name") String name) {
		return userApiKeyService.verifyCodeWhenGetSecretKey(time,name);
	}


}
